<?php
declare (strict_types=1);

namespace app\user\logic\user\admin;

use app\common\exception\LogicException;
use app\user\event\subscribes\user\Credit as CreditSubscribe;
use app\user\model\user\admin\CreditFlow as CreditFlowModel;
use think\facade\Event;

/**
 * Class CreditFlow
 *
 * @package app\user\logic\user\admin
 */
class CreditFlow extends CreditFlowModel
{
    /**
     * 流水记录
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/9/7
     *
     * @param        $uid
     * @param        $alter_type
     * @param        $num
     * @param        $credit
     * @param        $rel_type
     * @param        $rel_pk
     * @param string $note
     * @return $this
     */
    public function updateFlow($uid, $alter_type, $num, $credit, $rel_type, $rel_pk, $note = '')
    {
        $data = [
            'user_id'  => $uid,
            'type'     => $alter_type,
            'num'      => $num,
            'credit'   => $credit,
            'rel_type' => $rel_type,
            'rel_pk'   => $rel_pk,
            'note'     => $note,
        ];
        if (!$this->save($data)) {
            throw new LogicException('流水添加失败');
        }

        // 流水添加后处理事件
        Event::subscribe(CreditSubscribe::class);
        Event::trigger('CreditIncrease', $data);

        return $this;

    }
}